package com.yanqu.road.server.cmd.beautyescort;

import com.yanqu.road.entity.beautyescort.EscortBeautyDispatch;
import com.yanqu.road.entity.beautyescort.EscortBeautyEvent;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.beautyescort.BeautyEscortProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.beautyescort.BeautyEscortModule;
import com.yanqu.road.server.gameplayer.module.player.BeautyModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Cmd(code = Protocol.S_BEAUTY_ESCORT_ACCEPT_DISPATCH_BEAUTY_TEN_TIMES_FROM_CROSS, desc = "接镖+派美女(十连返回)")
public class BeautyEscortAcceptDispatchTenTimesCmdFromCross extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {

        BeautyEscortProto.BeautyEscortAcceptDispatchTenRespMsg.Builder clientMsg = BeautyEscortProto.BeautyEscortAcceptDispatchTenRespMsg.newBuilder();

        BeautyEscortProto.CrossBeautyEscortAcceptDispatchTenRespMsg respMsg = BeautyEscortProto.CrossBeautyEscortAcceptDispatchTenRespMsg.parseFrom(packet.getBytes());
        clientMsg.setRet(respMsg.getRet());

        if(!StringUtils.isNullOrEmpty(respMsg.getAllConsume())){
            player.getModule(PreReduceModule.class).restorePreReduce(respMsg.getAllConsume());
        }

        if(respMsg.getRet() == 0){

            //扣除消耗
            player.getModule(CurrencyModule.class).removeCurrency(PropertyHelper.parseStringToProperty(respMsg.getAllConsume()), eLogMoneyType.BeautyEscort,
                    eLogMoneyType.BeautyEscortDispatchTenTimesConsume);

            List<EscortBeautyDispatch> dispatchList = new ArrayList<>();
            for (Integer beautyId : respMsg.getBeautyIdList()) {
                EscortBeautyDispatch dispatch = new EscortBeautyDispatch();
                dispatch.setBeautyId(beautyId);
                dispatchList.add(dispatch);
            }
            //走镖奖励
            player.getModule(BeautyEscortModule.class).addDartReward(respMsg.getReward(), 0, dispatchList, 0);

            //事件奖励
            for (BeautyEscortProto.BeautyEscortEventHandleMsg handleMsg : respMsg.getEventList()) {
                EscortBeautyEvent beautyEvent = new EscortBeautyEvent();
                beautyEvent.setUnionUid(respMsg.getUnionUid());
                beautyEvent.setUnionLv(respMsg.getUnionLv());
                beautyEvent.setEventTime(DateHelper.getCurrentSecond());//事件生成时间点
                beautyEvent.setDamage(handleMsg.getDamage());
                beautyEvent.setEventId(handleMsg.getEventId());
                player.getModule(BeautyEscortModule.class).addEventReward(handleMsg.getReward(), 0, dispatchList, 0, beautyEvent);
            }

            clientMsg.addAllEvent(respMsg.getEventList());
            clientMsg.setReward(respMsg.getReward());

            //押镖任务
            player.notifyListener(eGamePlayerEventType.BeautyEscortTimes.getValue(),10);
        }

        clientMsg.addAllBeautyId(respMsg.getBeautyIdList());
        player.sendPacket(Protocol.U_BEAUTY_ESCORT_ACCEPT_DISPATCH_BEAUTY_TEN_TIMES, clientMsg);
    }

    @Override
    public void sendData(GamePlayer player) {

    }
}
